package com.af.security.mgt.account.password.token;

import com.af.security.mgt.user.User;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * @author : zhenyun.su
 * @since : 2019/6/25
 * @common : 密码重置令牌
 */

@Entity(name="sys_user_password_token")
public class UserPasswordToken {
    @Id
    private Long id;
    private String token;

    @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER)
    @JoinColumn(nullable = false, name = "user_id")
    private User user;

    private LocalDateTime createdDate;
    private LocalDateTime expiryDate;
    private Boolean enabled;

    public UserPasswordToken() {
    }

    public UserPasswordToken(String token, User user) {
        this.token = token;
        this.user = user;
        this.createdDate = LocalDateTime.now();
        this.enabled= true;
    }

    public void calculateExpiryDate(int expiryTimeInHour) {
        this.expiryDate = LocalDateTime.now().plusHours(expiryTimeInHour);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public LocalDateTime getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(LocalDateTime expiryDate) {
        this.expiryDate = expiryDate;
    }

    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public String toString() {
        return "PasswordResetToken{" +
                "id=" + id +
                ", token='" + token + '\'' +
                ", user=" + user +
                ", createdDate=" + createdDate +
                ", expiryDate=" + expiryDate +
                ", enabled=" + enabled +
                '}';
    }
}
